package com.cy.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cy.common.entity.Result;
import com.cy.pojo.Car;
import com.cy.pojo.vo.SearchVo;
import com.cy.service.CarService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @description 车辆信息
 * @author 残影
 * @date 2023-04-11
 */
@RestController
@RequestMapping("/car")
@Api(tags = "车辆信息模块")
public class CarController {
    @Autowired
    private CarService carService;

    /**
     * 根据ID查询车辆信息
     */
    @GetMapping("{id}")
    @ApiOperation("根据ID查询车辆信息")
    public Result findById(@PathVariable Integer id) {
        Car car = carService.getById(id);
        if (car != null) {
            return Result.ok("查询车辆信息成功", car);
        } else {
            return Result.failed(201, "没有该车辆信息");
        }
    }

    /**
    * 分页查询车辆信息
    */
    @ApiOperation("分页查询车辆信息")
    @GetMapping("/list")
    public Result list(SearchVo searchVo) {
        //分页构造器
        Page<Car> buildPage = new Page<>(searchVo.getPage(), searchVo.getLimit());
        //条件构造器
        QueryWrapper<Car> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(searchVo.getName())) {
            queryWrapper.like("", searchVo.getName());
        }
        Date startTime = searchVo.getStartTime();
        Date endTime = searchVo.getEndTime();
        if (ObjectUtils.isNotEmpty(startTime) && ObjectUtils.isNotEmpty(endTime)) {
            queryWrapper.between("create_time", startTime, endTime);
        }
        IPage<Car> pageList = carService.page(buildPage, queryWrapper);
        return Result.ok("查询成功", pageList.getRecords()).put("total", pageList.getTotal());
    }

    /**
     * 保存车辆信息
     */
    @PostMapping
    @ApiOperation("保存车辆信息")
    public Result save(@RequestBody Car car) {
        car.setCreateTime(new Date());
        car.setUpdateTime(new Date());
        boolean b = carService.save(car);
        if (b) {
            return Result.ok("保存用户车辆信息成功", car);
        } else {
            return Result.failed("保存车辆信息失败");
        }
    }

    /**
     * 修改车辆信息
     */
    @PutMapping
    @ApiOperation("修改车辆信息")
    public Result update(@RequestBody Car car) {
        car.setUpdateTime(new Date());
        boolean b = carService.saveOrUpdate(car);
        if (b) {
            return Result.ok("修改车辆信息成功", car);
        } else {
            return Result.failed("修改车辆信息失败");
        }
    }

    /**
     * 删除车辆信息
     */
    @DeleteMapping("{id}")
    @ApiOperation("删除车辆信息")
    public Result delete(@PathVariable Integer id) {
        boolean b = carService.removeById(id);
        if (b) {
            return Result.ok("删除车辆信息成功");
        } else {
            return Result.failed("删除车辆信息失败");
        }
    }
}
